<?php
/**
 * Load All Materials has a tree
 */
use Fox\SalesDistribution\Sales\Inquiry\Messages;
use Fox\Node;
use Fox\MaterialManagement\Material;
use Fox\MaterialManagement\Material\Hierarchy;
$block = new Fox\Html\Block("bHiearchy",Msg(Material::BUNLDE_NAME,19));
$tree = new Fox\Html\Tree("hierarchyTree");
$tree->label(Msg(Messages::BUNDLE,Messages::ACTIVITIES));
$tree->fieldcat(array("id",array('keyid',"parentid"),"code","label","level"));
$tree->setFieldAttr(array("id","parentid","code","level"), "hidden",true);
$tree->setFieldAttr(array("id","parentid","code","label","level"), "SetHiddenInput",true);
$tree->setCoche(true);
$tree->setExpandLevel(0);


$cacheFileName = Material::treeCacheName();
if(is_file($cacheFileName)) {
 	$root = unserialize(file_get_contents($cacheFileName));
}
if(!isset($root)) {
	// Load All Hierarchy tree
	$root = new Node(Node::ROOT_NODE);
	foreach(Hierarchy::loadTree() as $hierarchy) {
		$code = $hierarchy['code'];
		if(!empty($hierarchy['parentid'])) {
			$code = $parents[$hierarchy['parentid']].$code;
			$matByHierarchy[$code] = $matByHierarchy[$parents[$hierarchy['parentid']]]->add(new Node(array(
													'id' => $code,
													'parentid' => '',
													'code' => $code,
													'label' => $hierarchy['label'],
													'level' => $hierarchy['level'])));
		} else {
			$matByHierarchy[$code] = $root->add(new Node(array(
													'id' => $code,
													'parentid' => '',
													'code' => $code,
													'label' => $hierarchy['label'],
													'level' => $hierarchy['level'])));
		}
		$parents[$hierarchy['id']] = $code;

	}
	$finder = new Material();
	$materials = $finder->find(null,array('hierarchy' => false));

	foreach($materials as $material) {
		$hierarchy = $material->hierarchy;
		//$h1 = substr($material->hierarchy,0,2);
		if(empty($hierarchy)) {
			continue;
		}
		
		$node = $matByHierarchy[$material->hierarchy]->add(new Node(array(
											'id' => $material->materialid,
											'parentid' => $material->hierarchy,
											'code' => $material->materialid,
											'label' => $material->text()->label,
											'level' => 3,
											)));
											
		/*
		if(empty($h1)) {
			continue;
		}
		if(!isset($matByHierarchy[$h1])) {
			$label = Hierarchy::findLabel($h1);
			// It's a new sub level
			$matByHierarchy[$h1] = $root->add(new Node(array(
												'id' => $h1,
												'parentid' => '',
												'code' => $h1,
												'label' => $label,
												'level' => 1)));
		}
		$h2 = substr($material->hierarchy,2,4);
		if(!empty($h2) && !isset($matByHierarchy[$h1.$h2])) {
			// It's a new sub level
			$matByHierarchy[$h1.$h2] = $matByHierarchy[$h1]->add(new Node(array(
											'id' => $h1.$h2,
											'parentid' => '',
											'code' => $h1.$h2,
											'label' => Hierarchy::findLabel($h1.$h2),
											'level' => 2,
											)));
		}
		$h3 = substr($material->hierarchy,2,4);
		
		$node = $matByHierarchy[$h1.$h2]->add(new Node(array(
											'id' => $material->materialid,
											'parentid' => $h1.$h2,
											'code' => $material->materialid,
											'label' => $material->text()->label,
											'level' => 3,
											)));
		*/
	}
	// Cache Mat Tree
	$directory = Material::treeCacheDirectory();
	if(!is_dir($directory)) {
		mkdir($directory,0700);
	}
	file_put_contents($cacheFileName,serialize($root));
}
$tree->initialize($root);
return $tree;